package org.indi.graduatemanagement.controller;


import cn.hutool.core.lang.Assert;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.indi.graduatemanagement.common.lang.Result;
import org.indi.graduatemanagement.entity.Employment;
import org.indi.graduatemanagement.service.EmploymentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author zcl
 * @since 2021-04-24
 */
@RestController
@RequiresAuthentication
@RequestMapping("/employment")
public class EmploymentController {

    @Autowired
    EmploymentService employmentService;

    @GetMapping("/{id}")
    @RequiresPermissions("search")
    @RequiresAuthentication //登录后可查询
    public Result getEmploymentById(@PathVariable("id") String id) {
        Employment employment = employmentService.getById(id);
        Assert.notNull(employment, "查询错误");
        return Result.successful(employment);
    }

    @PostMapping("/save")
    @RequiresPermissions("add")
    @RequiresAuthentication //登录后可查询
    public Result saveEmployment(@Validated @RequestBody Employment employment) {

        if (employmentService.getById(employment.getEmploymentStuId()) != null) {
            return Result.fail("信息已存在");
        }
        employmentService.save(employment);
        return Result.successful(null);
    }

    @PostMapping("/update")
    @RequiresPermissions("update")
    @RequiresAuthentication //登录后可查询
    public Result updateEmployment(@Validated @RequestBody Employment employment) {
        if(employmentService.getById(employment.getEmploymentStuId())==null){
            return Result.fail("信息不存在");
        }
        employmentService.updateById(employment);
        return Result.successful("修改成功");
    }

    @GetMapping("/all")
    @RequiresPermissions("search")
    @RequiresAuthentication //登录后可查询
    public Result getAllEmployment() {
        List<Employment> employmentList = employmentService.list();
        return Result.successful(employmentList);
    }

    @DeleteMapping("/{id}")
    @RequiresPermissions("delete")
    @RequiresAuthentication //登录后可查询
    public Result deleteEmploymentById(@PathVariable("id") String id) {
        if (employmentService.removeById(id))
            return Result.successful(null);
        else
            return Result.fail("删除失败");
    }
}
